<?php

namespace app\admin\model;

use think\Db;
use think\Model;


class Pmsroominfo extends Model
{
    // 表名
    protected $name = 'pms_room_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public function get_room_price_list($prefix, $id, $check_in_date, $check_out_date)
    {
        $m = $this->alias('ri')
            ->field('ri.id,ri.floor,rt.name,ri.room_no,rt.monday_price,rt.tuesday_price,rt.wednesday_price,rt.thursday_price,rt.friday_price,rt.saturday_price,rt.sunday_price,rt.hour_room_price,rt.timeout_price,rt.deposit,rt.remarks')
            ->join($prefix . 'pms_room_type rt', 'ri.room_type = rt.id')
            ->where('ri.id', $id)
            ->find();

        $spObject = [];
        $spList   = $this->alias('ri')
            ->field('sp.*')
            ->join($prefix . 'pms_room_type rt', 'ri.room_type = rt.id')
            ->join($prefix . 'pms_special_price sp', 'sp.room_type_id = rt.id')
            ->where('ri.id', $id)
            ->where('sp.state', 1)
            ->order('sp.weigh desc')
            ->select();
        foreach ($spList as $item) {
            for ($i = 0; $i < 999999; $i++) {
                $d   = strtotime('+' . $i . ' day', $item['start_date']);
                $day = date('Y-m-d', $d);

                $key = $day;
                if (!isset($spObject[$key])) {
                    $spObject[$key] = $item;
                }

                if ($day == date('Y-m-d', $item['end_date'])) {
                    break;
                }
            }
        }


        $rows = [];
        for ($i = 0; $i < 999; $i++) {
            $d   = strtotime('+' . $i . ' day', strtotime($check_in_date));
            $day = date('Y-m-d', $d);
            $w   = date("w", $d);

            $price         = 0;
            $special_price = 0;
            switch ($w) {
                case 0:
                    $price = $m['sunday_price'];
                    break;
                case 1:
                    $price = $m['monday_price'];
                    break;
                case 2:
                    $price = $m['tuesday_price'];
                    break;
                case 3:
                    $price = $m['wednesday_price'];
                    break;
                case 4:
                    $price = $m['thursday_price'];
                    break;
                case 5:
                    $price = $m['friday_price'];
                    break;
                case 6:
                    $price = $m['saturday_price'];
                    break;
            }

            $key = $day;
            if (isset($spObject[$key])) {
                $price         = $spObject[$key]['price'];
                $special_price = 1;
            }

            array_push($rows, [
                'day'           => $day,
                'price'         => $price,
                'special_price' => $special_price
            ]);

            if ($day == $check_out_date) {
                break;
            }
        }

        return $rows;
    }

    /**
     * Description:
     * Function getValidRoom
     *
     * @author: dtt
     * @date  : 2021/9/20
     * @time  : 19:23
     */
    public function getRoomFullDate()
    {
        $list = $this->where('state', 1)
            ->where('is_dirty', 0)
            ->where('is_repair', 0)
            ->column('id,state,is_lock,room_status', 'id');

        if (empty($list)) {
            return [];
        }

        $unlock = array_filter($list, function ($item) {
            return $item['is_lock'] == 0;
        });

        if (empty($unlock)) {
            return 10001;
        }

        $order = (new Pmsorderinfo())->field('id,check_in_time,days,order_status,room_type_id,room_info_id')
            ->whereIn('order_status', [0,-1])->whereIn('room_info_id', array_column($unlock, 'id'))->select()->toArray();

        if (empty($order)) {
            return [];
        }

        $stayList = [];
        foreach ($order as $val) {
            if (!isset($unlock[$val['room_info_id']])) {
                continue;
            }
            for ($i = 0; $i < $val['days']; $i++) {
                $date = date('Y-m-d', strtotime("+{$i} day", $val['check_in_time']));
                if ($date !== date('Y-m-d')) {
                    $stayList[$val['room_info_id']][] = $date;
                }
            }
            isset($stayList[$val['room_info_id']]) && $stayList[$val['room_info_id']] = array_unique($stayList[$val['room_info_id']]);
            unset($i);
        }

        if (count($unlock) > count($stayList)) {
            $this->success('请求成功', []);
        }

        $res = array_shift($stayList) ?? [];
        foreach ($stayList as $val) {
            $res = array_intersect($res, $val);
        }

        return array_values($res);
    }

    /**
     * Description:
     * Function queryBookableRoomsByDate
     *
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: dtt
     * @date  : 2021/9/20
     * @time  : 20:13
     */
    public function queryBookableRoomsByDate($inTime,$outTime)
    {
        $rooms = [];
        $oi   = (new Pmsorderinfo());

        $roomList = Db::name('pms_room_info')
            ->where('state', 1)
            ->where('is_dirty', 0)
            ->where('is_repair', 0)
            ->select()->toArray();

        $reserveDates = setFormatDate($inTime, $outTime);
        foreach ($roomList as $r) {
            $orderList = $oi->where(['room_info_id' => $r['id']])
                ->whereIn('order_status', [-1,0])
                ->field('id,room_info_id,room_type_id,realname,check_in_time,check_out_time')
                ->select()->toArray();
            // 当前房间没有订单即为可预订
            if (empty($orderList)) {
                array_push($rooms, $r);
                continue;
            }
            foreach ($orderList as $orderI) {
                $orderI['date'] = setFormatDate($orderI['check_in_time'], $orderI['check_out_time']);

                if (!empty(array_intersect($reserveDates, $orderI['date']))) {
                    continue 2;
                }
            }
            array_push($rooms, $r);
        }
        // halt($rooms);
        return $rooms;
    }
}
